//
//  YLCarouselView.m
//  Thinkape-iOS
//
//  Created by admin on 16/4/28.
//  Copyright © 2016年 TIXA. All rights reserved.
//

#import "YLCarouselView.h"
#import "UIimageView+WebCache.h"

@interface YLCarouselView ()<UIScrollViewDelegate>
@property BOOL hasTimer;
@property (nonatomic,assign) NSUInteger interval;
@property (nonatomic,strong)UIImage *placeHolder;
@property (nonatomic,copy)NSArray *imageArray;
@property (nonatomic,strong)UIScrollView *wheelScrollView;
@property (nonatomic,strong) UIPageControl *wheelPageControl;
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,assign)NSUInteger currentImageIndex;
@property (nonatomic,strong)UIImageView *image1;
@property (nonatomic,strong)UIImageView *image2;
@property (nonatomic,strong)UIImageView *image3;
@property (nonatomic,assign)NSUInteger imageNum;
@property (nonatomic,strong)UIImageView *mask;
/**是否是本地图片
 *  yes:    no:需要从网络请求
 */
@property (nonatomic,assign)BOOL isLocal;
@end


@implementation YLCarouselView


+ (instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)array hasTimer:(BOOL)hasTimer interval:(NSUInteger)interval
{
    YLCarouselView *carousel = [[YLCarouselView alloc]initWithFrame:frame];
    carousel.hasTimer = hasTimer;
    carousel.interval = interval;
    [carousel setupWithArray:array];
    return carousel;
}

+ (instancetype)initWithFrame:(CGRect)frame hasTimer:(BOOL)hasTimer interval:(NSUInteger)interval placeHolder:(UIImage *)image
{
    
    YLCarouselView *carousel = [[YLCarouselView alloc]initWithFrame:frame];
    carousel.placeHolder = image;
    carousel.hasTimer = hasTimer;
    carousel.interval = interval;
    carousel.mask.image = image;
    return carousel;
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.mask = [[UIImageView alloc]initWithFrame:frame];
        [self addSubview:self.wheelScrollView];
        [self addSubview:self.wheelPageControl];
        [self addSubview:self.mask];
        self.wheelScrollView.scrollEnabled = NO;
    }
    return self;
}

//传网络url数组：
- (void) setupWithArray:(NSArray *)array
{
    self.imageArray = array;
    [self publicCall];

}

- (void) publicCall
{
    self.wheelScrollView.scrollEnabled = YES;
    self.mask.hidden = YES;
    
    self.imageNum = self.imageArray.count;
    self.currentImageIndex = 0;
    if (self.imageNum == 1) {
        self.wheelPageControl.hidden = YES;
        self.wheelScrollView.scrollEnabled = NO;
    }
    
    [self setup];
    
}
//传本地图片数组：
- (void) setupWithLocalImgArray:(NSArray *)array
{
    self.isLocal = YES;
    self.imageArray = array;
    [self publicCall];
}


- (void) setup
{
    self.wheelPageControl.pageIndicatorTintColor = [UIColor whiteColor];
    self.wheelPageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    CGPoint point = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.8);
    self.wheelPageControl.center = point;
    if (self.hasTimer) {
        [self setupTimer];
        
    }
    [self updateImg];
}

- (void) setupTimer
{
    if (self.interval == 0) {
        self.interval = 3;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(updateWheel) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop ]addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}
- (void) updateWheel
{
    CGPoint offset = self.wheelScrollView.contentOffset;
    offset.x += self.wheelScrollView.frame.size.width;
    [self.wheelScrollView setContentOffset:offset animated:YES];
    NSLog(@"%f",offset.x);
    
}
- (void) updateImg
{
    self.imageNum = self.imageArray.count;
    self.wheelPageControl.numberOfPages = self.imageNum;
    [self updateScrollImage];
    
    
}
- (void) updateScrollImage
{
    int left;
    int right;
    int page = self.wheelScrollView.contentOffset.x / self.wheelScrollView.frame.size.width;
    if (page == 0) {
        self.currentImageIndex = (self.currentImageIndex + self.imageNum - 1) % self.imageNum;
    }else if (page == 2){
        self.currentImageIndex = (self.currentImageIndex + 1) % self.imageNum;
    }
    left = (int)(self.currentImageIndex + self.imageNum - 1 ) % self.imageNum;
    right = (int)(self.currentImageIndex + 1) % self.imageNum;
    if (self.isLocal) {
        self.image1.image = [UIImage imageNamed:self.imageArray[left]];
        self.image2.image = [UIImage imageNamed:self.imageArray[self.currentImageIndex]];
        self.image3.image = [UIImage imageNamed:self.imageArray[right]];
    }else{
      
        [self.image1 sd_setImageWithURL:[NSURL URLWithString:self.imageArray[left]] placeholderImage:self.placeHolder];
        [self.image2 sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.currentImageIndex]] placeholderImage:self.placeHolder];
    }
    self.wheelPageControl.currentPage = self.currentImageIndex;
    [self.wheelScrollView setContentOffset:CGPointMake(self.wheelScrollView.frame.size.width, 0) animated:NO];
}


- (void) touch
{
    if ([self.delegate respondsToSelector:@selector(carouselTouch:atIndex:)]) {
        [self.delegate carouselTouch:self atIndex:self.currentImageIndex];
    }
}


- (void) destroy
{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)restart
{
    if (self.hasTimer && self.timer == nil) {
        [self setupTimer];
    }
}

#pragma mark - UIScrollView
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self updateScrollImage];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updateScrollImage];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self setupTimer];
}

#pragma mark  -- Getter
- (UIScrollView *)wheelScrollView
{
    if (!_wheelScrollView) {
        _wheelScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        
        _wheelScrollView.backgroundColor = [UIColor clearColor];
        _wheelScrollView.delegate = self;
        _wheelScrollView.showsVerticalScrollIndicator = NO;
        _wheelScrollView.showsHorizontalScrollIndicator = NO;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touch)];
        [_wheelScrollView addGestureRecognizer:tap];
        
        _image1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _image2 = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        _image3 = [[UIImageView alloc] initWithFrame:CGRectMake(2*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
        _image2.image = self.placeHolder;
        
        for (UIImageView *img in @[_image1,_image2,_image3]) {
            [_wheelScrollView addSubview:img];
        }
        [_wheelScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        _wheelScrollView.scrollEnabled = YES;
         _wheelScrollView.pagingEnabled = YES;
        _wheelScrollView.contentSize = CGSizeMake(3*self.frame.size.width, self.frame.size.height);
        
    }
    
    return _wheelScrollView;
}


-(UIPageControl *)wheelPageControl{
    if (!_wheelPageControl) {
        _wheelPageControl = [[UIPageControl alloc] init];
        [_wheelPageControl setBackgroundColor:[UIColor clearColor]];
        _wheelPageControl.currentPage = 0;
        _wheelPageControl.numberOfPages = self.imageNum;
    }
    return _wheelPageControl;
}



@end
